package com.cyt.wx.service.impl;

import cn.hutool.core.util.StrUtil;
import com.cyt.base.model.dto.ReturnResult;
import com.cyt.base.util.BaseConstant;
import com.cyt.base.util.DictCode;
import com.cyt.base.util.ErrorCode;
import com.cyt.sys.model.po.SysUser;
import com.cyt.sys.service.impl.BaseServiceImpl;
import com.cyt.wx.dao.WxApplyUseDao;
import com.cyt.wx.model.po.WxApplyUse;
import com.cyt.wx.model.vo.WxApplyUseVo;
import com.cyt.wx.service.WxApplyUseCategoryService;
import com.cyt.wx.service.WxApplyUseService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.io.UnsupportedEncodingException;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * 应用信息-应用信息Service层实现类
 *
 * @author shengQiang Yu
 * @date 2019-8-8 8:45:12
 */
@Service("wxApplyUseService")
public class WxApplyUseServiceImpl extends BaseServiceImpl<WxApplyUse> implements WxApplyUseService {

    @Resource
    private WxApplyUseCategoryService wxApplyUseCategoryService;
    @Resource
    private WxApplyUseDao wxApplyUseDao;

    @Autowired
    public void setBaseDao(WxApplyUseDao wxApplyUseDao) {
        this.baseDao = wxApplyUseDao;
    }

    /**
     * 查询对象集合
     *
     * @param wxApplyUseVo 对象
     * @return List 对象集合
     * @author shengQiang Yu
     * @date 2019-8-8 8:45:12
     */
    @Override
    public List<WxApplyUse> listExample(WxApplyUseVo wxApplyUseVo) {
        Example example = new Example(WxApplyUse.class);
        example.setOrderByClause(" create_time desc");
        Example.Criteria criteria = example.createCriteria();
        if (StrUtil.isNotEmpty(wxApplyUseVo.getName())) {
            criteria.andLike("name", "%" + wxApplyUseVo.getName() + "%");
        }
        if (StrUtil.isNotEmpty(wxApplyUseVo.getStatus())) {
            criteria.andEqualTo("status", wxApplyUseVo.getStatus());
        }
        if (StrUtil.isNotEmpty(wxApplyUseVo.getApplyUseStatus())) {
            criteria.andEqualTo("applyUseStatus", wxApplyUseVo.getApplyUseStatus());
        }
        criteria.andEqualTo("removeTag", DictCode.BASE_REMOVE_TAG_NORMAL);
        return wxApplyUseDao.selectByExample(example);
    }

    /**
     * 分页查询
     *
     * @param wxApplyUseVo 接受参数对象
     * @param pageNum          分页数
     * @param pageSize         每页显示条数
     * @return ReturnResult
     * @author shengQiang Yu
     * @date 2019-8-8 8:45:12
     */
    @Override
    public ReturnResult page(WxApplyUseVo wxApplyUseVo, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        PageHelper.orderBy(" create_time desc");
        List<WxApplyUse> list = listExample(wxApplyUseVo);
        PageInfo<WxApplyUse> page = new PageInfo<>(list);
        return new ReturnResult(page.getList(), page.getTotal(), page.isHasNextPage());
    }

    /**
     * 添加或更新
     *
     * @param wxApplyUseVo 参数接受对象
     * @param sysUser      当前用户
     * @return ReturnResult
     * @author shengQiang Yu
     * @date 2019-8-8 8:45:12
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ReturnResult addOrUpdate(WxApplyUseVo wxApplyUseVo, SysUser sysUser) {
        if (StrUtil.isEmpty(wxApplyUseVo.getId())) {
            WxApplyUse wxApplyUse = new WxApplyUse(sysUser);
            wxApplyUse.setName(wxApplyUseVo.getName());
            wxApplyUse.setCoverImage(wxApplyUseVo.getCoverImage());
            if(StrUtil.isNotEmpty(wxApplyUseVo.getWebPath())){
                try {
                    java.net.URLDecoder urlDecoder=new java.net.URLDecoder();
                    wxApplyUse.setWebPath(urlDecoder.decode(wxApplyUseVo.getWebPath(),"utf-8"));
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
            }else{
                wxApplyUse.setWebPath("");
            }
            if(StrUtil.isNotEmpty(wxApplyUseVo.getPhonePath())){
                try {
                    java.net.URLDecoder urlDecoder=new java.net.URLDecoder();
                    wxApplyUse.setPhonePath(urlDecoder.decode(wxApplyUseVo.getPhonePath(),"utf-8"));
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
            }else{
                wxApplyUse.setPhonePath("");
            }
            wxApplyUse.setMoney(wxApplyUseVo.getMoney());
            wxApplyUse.setUseDay(wxApplyUseVo.getUseDay());
            wxApplyUse.setStatus(wxApplyUseVo.getStatus());
            wxApplyUse.setType(wxApplyUseVo.getType());
            wxApplyUse.setVersion(wxApplyUseVo.getVersion());
            wxApplyUse.setClicks(wxApplyUseVo.getClicks());
            wxApplyUse.setApplyUseStatus(wxApplyUseVo.getApplyUseStatus());
            wxApplyUse.setUpdateRecord(wxApplyUseVo.getUpdateRecord());
            wxApplyUse.setDescription(wxApplyUseVo.getDescription());
            int num = wxApplyUseDao.insert(wxApplyUse);
            if (num > 0) {
                // 分类保存
                String categoryIds = wxApplyUseVo.getCategoryIds();
                wxApplyUseCategoryService.batchSave(wxApplyUse.getId(), categoryIds, sysUser);
                return new ReturnResult(BaseConstant.DATABASE_SUCCEED_INSERT);
            } else {
                return new ReturnResult(ErrorCode.CODE_1, BaseConstant.DATABASE_ERROR_INSERT);
            }
        } else {
            WxApplyUse wxApplyUse = wxApplyUseDao.selectByPrimaryKey(wxApplyUseVo.getId());
            wxApplyUse.setName(wxApplyUseVo.getName());
            wxApplyUse.setCoverImage(wxApplyUseVo.getCoverImage());
            if(StrUtil.isNotEmpty(wxApplyUseVo.getWebPath())){
                try {
                    java.net.URLDecoder urlDecoder=new java.net.URLDecoder();
                    wxApplyUse.setWebPath(urlDecoder.decode(wxApplyUseVo.getWebPath(),"utf-8"));
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
            }else{
                wxApplyUse.setWebPath("");
            }
            if(StrUtil.isNotEmpty(wxApplyUseVo.getPhonePath())){
                try {
                    java.net.URLDecoder urlDecoder=new java.net.URLDecoder();
                    wxApplyUse.setPhonePath(urlDecoder.decode(wxApplyUseVo.getPhonePath(),"utf-8"));
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
            }else{
                wxApplyUse.setPhonePath("");
            }


            wxApplyUse.setMoney(wxApplyUseVo.getMoney());
            wxApplyUse.setUseDay(wxApplyUseVo.getUseDay());
            wxApplyUse.setApplyUseStatus(wxApplyUseVo.getApplyUseStatus());
            wxApplyUse.setStatus(wxApplyUseVo.getStatus());
            wxApplyUse.setType(wxApplyUseVo.getType());
            wxApplyUse.setVersion(wxApplyUseVo.getVersion());
            wxApplyUse.setClicks(wxApplyUseVo.getClicks());
            wxApplyUse.setUpdateRecord(wxApplyUseVo.getUpdateRecord());
            wxApplyUse.setDescription(wxApplyUseVo.getDescription());
            wxApplyUse.setUpdateTime(new Date());
            wxApplyUse.setUpdateUser(sysUser.getId());
            int num = wxApplyUseDao.updateByPrimaryKey(wxApplyUse);
            if (num > 0) {
                // 分类保存
                String categoryIds = wxApplyUseVo.getCategoryIds();
                wxApplyUseCategoryService.batchSave(wxApplyUse.getId(), categoryIds, sysUser);
                return new ReturnResult(BaseConstant.DATABASE_SUCCEED_UPDATE);
            } else {
                return new ReturnResult(ErrorCode.CODE_1, BaseConstant.DATABASE_ERROR_UPDATE);
            }
        }
    }

    /**
     * 根据条件逻辑删除
     *
     * @param wxApplyUseVo 参数接受对象
     * @param sysUser 当前用户
     * @return ReturnResult
     * @author shengQiang Yu
     * @date 2019-8-8 8:45:12
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ReturnResult remove(WxApplyUseVo wxApplyUseVo, SysUser sysUser) {
        //要修改的字段
        WxApplyUse wxApplyUse = new WxApplyUse();
        wxApplyUse.setRemoveTag(BaseConstant.REMOVE_TAG_YES);
        wxApplyUse.setUpdateUser(sysUser.getId());
        wxApplyUse.setUpdateTime(new Date());
        //筛选条件
        Example example = new Example(WxApplyUse.class);
        Example.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotBlank(wxApplyUseVo.getId())) {
            criteria.andIn("id", Arrays.asList(wxApplyUseVo.getId().split(",")));
        }
        criteria.andEqualTo("removeTag", BaseConstant.REMOVE_TAG_NO);
        wxApplyUseDao.updateByExampleSelective(wxApplyUse, example);
        return new ReturnResult(ErrorCode.CODE_0, BaseConstant.DATABASE_SUCCEED_DEL);
    }

    /**
     * 根据条件物理删除
     *
     * @param wxApplyUseVo 参数接受对象
     * @param sysUser      当前用户
     * @return ReturnResult
     * @author shengQiang Yu
     * @date 2019-8-8 8:45:12
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ReturnResult delete(WxApplyUseVo wxApplyUseVo, SysUser sysUser) {
        //筛选条件
        Example example = new Example(WxApplyUse.class);
        Example.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotBlank(wxApplyUseVo.getId())) {
            criteria.andIn("id", Arrays.asList(wxApplyUseVo.getId().split(",")));
        }
        wxApplyUseDao.deleteByExample(example);
        return new ReturnResult(ErrorCode.CODE_0, BaseConstant.DATABASE_SUCCEED_DEL);
    }

    @Override
    public List<WxApplyUse> selectByCondition(WxApplyUseVo wxApplyUseVo) {
        return wxApplyUseDao.selectByCondition(wxApplyUseVo);
    }
}